package com.codergd.gra.controller;

import com.alipay.api.AlipayApiException;
import com.codergd.gra.alipay.AlipayTradePrecreate;
import com.codergd.gra.alipay.AlipayTradeQuery;
import com.codergd.gra.bean.Job;
import com.codergd.gra.service.JobService;
import com.codergd.gra.utils.QRCode;
import com.codergd.gra.vo.JobCom;
import com.google.zxing.WriterException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/job")
public class JobController {
    @Autowired
    JobService jobService;
    @Autowired
    AlipayTradePrecreate alipayTradePrecreate;
    @Autowired
    AlipayTradeQuery alipayTradeQuery;
    @Autowired
    QRCode qrCode;

    //创建职位
    @PostMapping("/insertJob")
    public int insertJob(Job job){
        System.out.println("公司是："+job.com);
        Integer wit = 0;
//        if (jobService.selectWitByCom(job.com) != null)
//            wit = jobService.selectWitByCom(job.com);
//        job.setWit(wit);
        System.out.println("权重是："+job.getWit());
        return jobService.insertJob(job);
    }

    @PostMapping("/getJob")
    public List<Job> getJob(int com){
        return jobService.getJob(com);
    }
    @PostMapping("/getJobInfo")
    public Job getJobInfo(int id){
        return jobService.getJobInfo(id);
    }

    //获取职位信息根据id
    @PostMapping("/getJobById")
    public Job getJobById(int id){
        return jobService.getJobById(id);
    }

    //修改职位信息
    @PostMapping("/updaeJob")
    public int updateJob(Job job){
        return jobService.updaeJob(job);
    }

    //删除职位信息
    @PostMapping("/deleteJob")
    public int deleteJob(int id){
        return jobService.deleteJob(id);
    }

    //根绝个人信息获取职位
    @PostMapping("/getJobByUser")
    public List<JobCom> getJobByUser(int sal, String job,int sal1, String job1,String address){
        return jobService.getJobByUser(sal,job,sal1,job1,address);
    }

    @RequestMapping("/getJobByStr")
    public List<JobCom> getJobByStr(String str){
        System.out.println(str);
        return jobService.getJobByStr(str);
    }

//    //查询原来的权重
//    @PostMapping("/selectWit")
//    public int selectWit(int id){
//        return jobService.selectWit(id);
//    }
//
//    //修改支付后的权重
//    @PostMapping("/updateWit")
//    public int updateWit(int id,int wit){
//        return jobService.updateWit(id,wit);
//    }
    //创建订单
    @PostMapping("/createOrder")
    public Map<String,String> createOrder(float num) throws AlipayApiException, IOException, WriterException {
        Map<String,String> map = new HashMap();
        //订单号
        String no = String.valueOf(UUID.randomUUID()).replaceAll("-","");
        String qr = alipayTradePrecreate.creatOrder(no,num);
        qr = qrCode.generateQRCodeImage(qr,300,300);
        map.put("no",no);
        map.put("qr",qr);
        System.out.println(no);
        System.out.println(qr);
        return map;
    }

    //查询订单支付情况
    @PostMapping("/updateWit")
    public int updateWit(int id,String no,float num) throws AlipayApiException {
        System.out.println(id);
        System.out.println(no);
        System.out.println(num);
        String state = alipayTradeQuery.queryState(no);
        if (state.equals("Success")){
            int wit = jobService.selectWit(id);
            wit += (num*100);
            jobService.updateWit(id,wit);
            return 1;
        }else return 0;
    }

    //查询出权重不为0的职位
    @PostMapping("/selectByWit")
    public List<JobCom> getJobByWit(){
        return jobService.getJobByWit();
    }
}
